package com.lovo.bbs.bo;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import javax.naming.NamingException;

import com.lovo.bbs.dao.TopicDao;
import com.lovo.bbs.po.TopicPo;
import com.lovo.bbs.vo.TopicVo;

/**
 * 主题Bo
 * 
 * @author tiancen2001
 * 
 */
public class TopicBo {
	 //操作类型
	 public static final int OPER_OF_UPDATE=2;
	 public static final int OPER_OF_DELETE=3;
	/**
	 * 返回指定论坛下的主题Vo列表
	 * @param queryPage 
	 */
	public  ArrayList<TopicVo> getTopics(int forumid, int queryPage) {
		ArrayList<TopicVo> topicVos = new ArrayList<TopicVo>();
		ArrayList<TopicPo> topicPos = null;
		try {
			topicPos = new TopicDao().getTopics(forumid,queryPage);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		topicVos = po2vo(topicVos, topicPos);
		return topicVos;
	}

	/**
	 * 返回指定论坛下的置顶主题Vo列表
	 */
	public  ArrayList<TopicVo> getTopTopics(int forumid) {
		ArrayList<TopicVo> topTopicVos = new ArrayList<TopicVo>();
		ArrayList<TopicPo> topTopicPos = null;
		try {
			topTopicPos = new TopicDao().getTopTopics(forumid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		topTopicVos = po2vo(topTopicVos, topTopicPos);
		return topTopicVos;
	}
	
	/**
	 * 按给定条件搜索所有论坛下的主题
	 * @param forumid 
	 */
	public  ArrayList<TopicVo> switchSearch(String cond, int forumid){
		ArrayList<TopicVo> topicVos = new ArrayList<TopicVo>();
		ArrayList<TopicPo> topicPos = null;
		try {
			topicPos = new TopicDao().switchSearch(cond,forumid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		topicVos = po2vo(topicVos,topicPos);
		return topicVos;
	}
	

	/**
	 * 返回指定ID的Vo
	 */
	public  TopicVo getTopicInfo(int topicid) {
		TopicVo vo = new TopicVo();
		TopicPo po=null;
		try {
			po = new TopicDao().getTopicInfo(topicid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		vo.setAuthorid(po.getAuthorid());
		vo.setAuthorname(po.getAuthorname());
		vo.setAuthorHeadimg(po.getAuthorHeadimg());
		vo.setAuthorRankid(po.getAuthorRankid());
		vo.setAuthorScore(po.getAuthorScore());
		vo.setAuthorSignature(po.getAuthorSignature());
		
		
		vo.setForumid(po.getForumid());
		vo.setHits(po.getHits());
		vo.setAttach(po.isAttach());
		vo.setTop(po.isTop());

		vo.setBest(po.isBest());
		vo.setHighlight(po.isHighlight());
		vo.setLastPostAuthor(po.getLastPostAuthor());
		vo.setLastPostDate(po.getLastPostDate());
		vo.setPostNum(po.getPostNum());

		vo.setTopicid(po.getTopicid());
		vo.setTopicDate(po.getTopicDate());
		vo.setTopicTitle(po.getTopicTitle());
		
		vo.setContent(po.getContent());
		
		vo.setForumName(po.getForumName());
		
		vo.setUserTopicNum(po.getUserTopicNum());

		return vo;
	}

	/**
	 * 将Po列表转化为Vo列表
	 * 
	 * @param topicVos
	 * @param topicPos
	 */
	private  ArrayList<TopicVo> po2vo(ArrayList<TopicVo> vos,
			ArrayList<TopicPo> pos) {
		Iterator<TopicPo> it = pos.iterator();
		while (it.hasNext()) {
			TopicPo po = it.next();
			TopicVo vo = new TopicVo();

			vo.setAuthorid(po.getAuthorid());
			vo.setAuthorname(po.getAuthorname());
			vo.setForumid(po.getForumid());
			vo.setHits(po.getHits());
			vo.setAttach(po.isAttach());
			vo.setTop(po.isTop());

			vo.setBest(po.isBest());
			vo.setHighlight(po.isHighlight());
			vo.setLastPostAuthor(po.getLastPostAuthor());
			vo.setLastPostDate(po.getLastPostDate());
			vo.setPostNum(po.getPostNum());

			vo.setTopicid(po.getTopicid());
			vo.setTopicDate(po.getTopicDate());
			vo.setTopicTitle(po.getTopicTitle());
			
			vo.setContent(po.getContent());

			vo.setForumName(po.getForumName());
			
			vo.setUserTopicNum(po.getUserTopicNum());
			
			vos.add(vo);
		}
		return vos;
	}
	
	/**
	 * 发表新主题,返回新主题的ID
	 */
	public   int addOneTopic(TopicVo vo){
		TopicPo po = new TopicPo();
		po.setForumid(vo.getForumid());
		po.setTopicTitle(vo.getTopicTitle());
		po.setAuthorname(vo.getAuthorname());
		po.setContent(vo.getContent());
		int topicid=0;
		try {
			topicid = new TopicDao().addOneTopic(po);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return topicid;
	}
	
	/**
	 * 查看一次
	 */
	public  int topicHitted(int topicid){
		int added=0;
		try {
			added = new TopicDao().topicHitted(topicid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return added;
	}

	/**
	 * 指定主题新增一个回复
	 * @param postid
	 * @param topicid 
	 * @return
	 */
	public  int  addOnePost(int postid, int topicid) {
		int added = 0;
		try {
			added = new TopicDao().addOnePost(postid,  topicid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return added;
	}

	/**
	 * 查询指定论坛的主题数
	 * 参数为0时查询所有论坛主题总数
	 * @param forumid
	 * @return
	 */
	public  int getTopicNum(int forumid) {
		int topicNum=0;
		try {
			topicNum = new TopicDao().getTopicNum(forumid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return topicNum;
	}
	
	/**
	 * 主题控制台更新
	 */
	public  int updateTopic(TopicVo vo,int OperType){
		int updated=0;
		TopicPo po = new TopicPo();
		
		po.setTopicid(vo.getTopicid());
		po.setForumid(vo.getForumid());	
		po.setTopicTitle(vo.getTopicTitle());
        po.setHits(vo.getHits());
		po.setPostNum(vo.getPostNum());
		po.setHighlight(vo.isHighlight());
		po.setBest(vo.isBest());
		po.setTop(vo.isTop());

		try {
			updated = new TopicDao().updateTopic(po,OperType);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
	}

	/**
	 * 主题回复数减一
	 * @param topicid
	 */
	public  int  delOnePost(int topicid) {
		int updated = 0;
		try {
			updated = new TopicDao().delonePost(topicid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return updated;
		
	}

	/**
	 * 计算主题最后回复
	 * @param topicid
	 */
	public  int  setLastPost(int topicid) {
		int setted  = 0;
		try {
			setted = new TopicDao().setLastPost(topicid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return setted;
	} 
	/**
	 * 删除该用户的主题
	 */
	public  int delTopicByUserID(int userid){
		int del = 0;
		try {
			del = new TopicDao().delTopicByUserID(userid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return del;
	}
	/**
	 * 计算主题的回复数
	 */
	public  int calcPostNum(int topicid){
		int calced = 0;
		try {
			calced = new TopicDao().calcPostNum(topicid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return calced;
	}
	/**
	 * 返回该主题的论坛ID
	 */
	public  int getForumID(int topicid){
		int forumid=0;
		try {
			forumid = new TopicDao().getForumID(topicid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return forumid;
	}
	
	/**
	 * 删除该用户的主题,返回主题所属的论坛
	 */
	public  HashSet<Integer> delTopics(int userid){
		HashSet<Integer> set = new HashSet<Integer>();
		try {
			set = new TopicDao().delTopics(userid);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return set;
	}
}
